package com.gzhryc.shared_device.oem.h5.agent.controllers.api.v1.team;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.gzhryc.common.NumberTools;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.oem.code.agent.dao.db.AgentRelationWallet;
import com.gzhryc.shared_device.oem.code.agent.services.AgentRelationWalletService;
import com.gzhryc.shared_device.oem.h5.agent.ClientAPICodeConstants;
import com.gzhryc.shared_device.oem.h5.agent.ELoginUserRoleType;
import com.gzhryc.shared_device.oem.h5.agent.LoginUser;
import com.gzhryc.shared_device.oem.h5.agent.WebConstants;
import com.gzhryc.shared_device.oem.h5.agent.dto.api.v1.team.TeamUserWalletResponse;

public class TeamUserWalletController {

    @Route(value = "/agent/api/v1/team/user/wallet/info.data")
    public TeamUserWalletResponse login(HttpServletRequest request) {
        TeamUserWalletResponse apiResp = new TeamUserWalletResponse(ClientAPICodeConstants.NULL_DATA,"暂无钱包信息");
        LoginUser loginUser = WebUtils.getAttribute(request, WebConstants.ATTR_LOGIN_USER,LoginUser.class);
        if(loginUser != null){
        	if(ELoginUserRoleType.Partner.index().equals(loginUser.getRoleType()) 
            		|| ELoginUserRoleType.Dealer.index().equals(loginUser.getRoleType())) {
	            List<AgentRelationWallet> walletList = AgentRelationWalletService.self().findJoinByUserKeys(loginUser.getParentId(),loginUser.getId());
	            if(walletList != null && walletList.size() > 0){
	                apiResp.setCode(ClientAPICodeConstants.SUCCESS);
	                apiResp.getBody().setItems(new ArrayList<>());
	
	                Long money = 0L;
	                Long withdrawnMoney = 0L;       //已提现金额
	                for(AgentRelationWallet wallet : walletList) {
	                    money = money + wallet.getMoney();
	                    withdrawnMoney = withdrawnMoney + wallet.getWithdrawnMoney();
	
	                    TeamUserWalletResponse.Item item = new TeamUserWalletResponse.Item();
	                    item.setAccountGroupId(wallet.getAccountGroupId());
	                    item.setAccountGroupName(wallet.getAccountGroupName());
	                    item.setMoney(NumberTools.getMoneyStr(NumberTools.changeMoneyLi(wallet.getMoney())));
	                    apiResp.getBody().getItems().add(item);
	                }
	
	                apiResp.getBody().setMoney(NumberTools.getMoneyLiStr(money));
	                apiResp.getBody().setWithdrawnMoney(NumberTools.getMoneyLiStr(withdrawnMoney));
	            }
        	}
        }
        return apiResp;
    }
}
